package vpfarm.slave;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import jtools.files.TextFile;
import jtools.files.filesystem.FileOperation;


public class Blender {
	
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		File f = new File("tmp");
		if (f.exists())
			FileOperation.deleteDir(f);
		FileOperation.createDir(f, true);
		File out = File.createTempFile("out", null, new File("tmp"));
		render("C:\\Programme\\Blender Foundation\\Blender\\blender.exe", new File("plane.blend"), new File("result"), "JPG", "_", 1, 2, out);
		ArrayList<File> res = parseOutputFile(out);
		for (int i = 0; i < res.size(); i++){
			System.out.println(res.get(i));
		}
		System.out.println("Fertig");
	}
	
	
	public static void runCommand(String s, File output){
		try {
			TextFile tf = new TextFile();
			Runtime rt = Runtime.getRuntime();
			Process pr = rt.exec(s);
			
			BufferedReader procout = new BufferedReader(new InputStreamReader(pr.getInputStream()));
			String line;
			while ((line = procout.readLine()) != null) {
				System.out.println("  OUT> " + line);
				tf.add(line);
			}
			tf.writeFile(output.toString(), true);
			tf.closeFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	private static void renderWindows(String blenderExe, File blenderFile, File targetDir, String format, String pref, int start, int end, File outputFile){
		String s= "\"";
		StringBuffer sb = new StringBuffer();
		sb.append(s+blenderExe+s);
		sb.append(" -b "+s+blenderFile.getAbsolutePath()+s);
		sb.append(" -F "+format);
		sb.append(" -x 1 -o "+s+targetDir.getAbsolutePath()+File.separator+pref+s);
		sb.append(" -s "+start);
		sb.append(" -e "+end);
		sb.append(" -t 4");
		sb.append(" -a");
		String[] tmp = {"-waitfor", "-catchoutput", sb.toString()};
		runCommand(sb.toString(), outputFile);
	}
	
	private static void renderLinux(String blenderExe, File blenderFile, File targetDir, String format, String pref, int start, int end, File outputFile){
		String s= "";
		StringBuffer sb = new StringBuffer();
		sb.append(s+blenderExe+s);
		sb.append(" -b "+s+blenderFile.getAbsolutePath()+s);
		sb.append(" -F "+format);
		sb.append(" -x 1 -o "+s+targetDir.getAbsolutePath()+File.separator+pref+s);
		sb.append(" -s "+start);
		sb.append(" -e "+end);
		sb.append(" -t 4");
		sb.append(" -a");
		String[] tmp = {"-waitfor", "-catchoutput", sb.toString()};
		runCommand(sb.toString(), outputFile);
	}
	
	
	
	public static int render(String blenderExe, File blenderFile, File targetDir, String format, String pref, int start, int end, File outputFile){
		 String osName = System.getProperty("os.name").toLowerCase();
		 if (osName.toLowerCase().contains("win"))
			 renderWindows(blenderExe, blenderFile, targetDir, format, pref, start, end, outputFile);
		 else
			 renderLinux(blenderExe, blenderFile, targetDir, format, pref, start, end, outputFile);
		return 0;
	}


	public static ArrayList<File> parseOutputFile(File output) throws IOException{
		TextFile tf = new TextFile();
		tf.readFile(output, false);
		ArrayList<File> resultList = new ArrayList<File>();
		while(tf.hasNext()){
			String line = tf.getNextLine();
			if (line.contains("Saved:")){
				String n = line;
				n = n.replace("Saved: ", "");
				n = n.replace(" Time:", "");
				n = n.substring(0, n.length()-9);
				File f = new File(n);
				if (f.exists())
					resultList.add(f);
			}
		}
		tf.closeFile();
		return resultList;
		
	}
}
